package com.text;

import com.dao.user.UserMapper;
import com.entity.Address;
import com.entity.MyBatis;
import com.entity.User;
import com.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.security.Principal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.log4j.Logger;
public class UserMapperTest {
    private Logger logger =Logger.getLogger(UserMapperTest.class);
    public void testGetUserList(){
        SqlSession sqlSession = null;
        List<User> userList = new ArrayList<User>();
        try{
            sqlSession = MyBatisUtil.createSqlSession();
//            String userName = "";
//            Integer roleId = null;
//            Integer pageSize =5;
//            Integer currentPageNo = 0;
            User user = new User();
            user.setUserName("李");
            user.setUserRole(3);
            //Map<String,String> userMap = new HashMap<String,String>;
            //userMap.put("uNmae","李");
            //userMap.put("uRole","3");
            //userList = sqlSession.getMapper(UserMapper.class).getUserListByMap(userMap);
            String userName = "王";
            Integer roleId = 3;
            userList = sqlSession.getMapper(UserMapper.class).getUserList(userName,roleId);
           // userList = sqlSession.getMapper(UserMapper.class).getUserList(userName,roleId,pageSize,currentPageNo);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        for(User user:userList){
            logger.debug("testGetUserList=====>id:"+user.getId()+"and userCode:" +user.getUserCode()
                        +"and userName"+user.getUserName()+"and userRole"+user.getUserRole()
                        +"and userRoleName" +user.getUserRoleName()+"and age"+user.getAge()
                        +"and phone:"+user.getPhone()+"and gende"+user.getGender());
        }
    }
    public void testAdd(){
        logger.debug("testAdd !==============");
        SqlSession sqlSession =null;
        int count =0;
        try{
            sqlSession = MyBatisUtil.createSqlSession();
            User user = new User();
            user.setUserCode("test001");
            user.setUserName("测试用户001");
            user.setUserPassword("1234567");
            Date birthday = new SimpleDateFormat("yyyy-MM-dd").parse("1984-12-12");
            user.setBirthday(birthday);
            user.setAddress("地址测试");
            user.setGender(1);
            user.setPhone("13688783697");
            user.setUserRole(1);
            user.setCreatedBy(1);
            user.setCreationDate(new Date());
            count = sqlSession.getMapper(UserMapper.class).add(user);
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
            count = 0;
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        logger.debug("testAdd count:" +count);
    }
    public void testModify(){
        logger.debug("testModify !===============");
        SqlSession sqlSession =null;
        int count = 0;
        try{
            User user =new User();
            user.setId(25);
            user.setUserCode("testmodify");
            user.setUserName("测试用户修改");
            user.setUserPassword("000000");
            Date birthday = new SimpleDateFormat("yyyy-MM-dd").parse("1980-10-10");
            user.setBirthday(birthday);
            user.setAddress("地址测试修改");
            user.setGender(2);
            user.setPhone("13600002222");
            user.setUserRole(2);
            user.setModify(1);
            user.setModifyDate(new Date());
            sqlSession =MyBatisUtil.createSqlSession();
            count= sqlSession.getMapper(UserMapper.class).modify(user);
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.commit();
            count = 0;
        }finally{
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        logger.debug("testModify count:"+count);
    }
    public void testUpdatePwd(){
        logger.debug("testUpdatePwd !=================");
        SqlSession sqlSession =null;
        String pwd ="8888888";
        Integer id = 1;
        int count = 0;
        try{
            sqlSession = MyBatisUtil.createSqlSession();
            count =sqlSession.getMapper(UserMapper.class).updatePwd(id,pwd);
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
            count = 0;
        }finally{
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        logger.debug("textUpdatePwd count:"+count);
    }
    public void testDelectUserById(){
        logger.debug("testDeleeUserById!===================");
        SqlSession sqlSession = null;
        Integer delId =25;
        int count = 0;
        try{
            sqlSession = MyBatisUtil.createSqlSession();
            count = sqlSession.getMapper(UserMapper.class).deleteUserById(delId);
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
            count = 0;
        }finally{
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        logger.debug("textDelectUserById count:"+count);
    }
    public void getUserListByRoleIdTest(){
        SqlSession sqlSession = null;
        List<User> userList = new ArrayList<User>();
        Integer roleId = 3;
        try{
           sqlSession =MyBatisUtil.createSqlSession();
           userList = sqlSession.getMapper(UserMapper.class).getUserListByRoleId(roleId);
        }catch (Exception e){
            e.printStackTrace();
        }finally{
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        logger.debug("getUserListByRoleIdTest userList.size:"+userList.size());
        for(User user:userList){
            logger.debug("userLise ===>userName:"+user.getUserName()+",Role"+user.getRole().getId()+",---"+user.getRole().getRoleCode()+"---"+user.getRole().getRoleName());
        }
    }
    public void getAddressListByUserIdTest(){
        SqlSession sqlSession = null;
        List<User> userList = new ArrayList<User>();
        Integer roleId = 1;
        try{
            sqlSession =MyBatisUtil.createSqlSession();
            userList = sqlSession.getMapper(UserMapper.class).getAddressListByUserId(roleId);
        }catch (Exception e){
            e.printStackTrace();
        }finally{
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        for(User user:userList){
            logger.debug("userList(include:addresslist)===>userCode:"+user.getUserCode()+",userName:"+user.getUserName());
        }
    }

    public void testGetUserByRoleId_foreach_array(){
        SqlSession sqlSession = null;
        List<User> userList = new ArrayList<User>();
        Integer[] roleIds ={2,3};
        try{
            sqlSession = MyBatisUtil.createSqlSession();
            userList = sqlSession.getMapper(UserMapper.class).getUserByRoleId_foreach_array(roleIds);
        }catch (Exception e){
            e.printStackTrace();
        }finally{
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        logger.debug("userList.size---->"+userList.size());
        for (User user:userList){
            logger.debug("user =====> id:"+user.getId()+
                            ",userCode:"+user.getUserCode()+
                            ",userName:"+user.getUserName()+
                            ",userRole:"+user.getUserRole());
        }
    }
    public void testGetUserByRoleId_foreach_list(){
        SqlSession sqlSession = null;
        List<User> userList = new ArrayList<User>();
        Integer[] roleIds ={2,3};
        List<Integer> roleList = new ArrayList<Integer>();
        roleList.add(2);
        roleList.add(3);
        userList = sqlSession.getMapper(UserMapper.class).getUserByRoleId_foreach_array(roleIds);
    }

}


